BASE=$(cd $(dirname $0);pwd) && clear

dir_workspace=$BASE
dir_build=$BASE/build
dir_src=$BASE/src
dir_tar=$BASE/tar
dir_status=$BASE/status
dir_out=$BASE/out

common_ext=".tar.gz"
name_openssl_301="openssl-openssl-3.0.1"
name_openssl_111m="openssl-OpenSSL_1_1_1m"

file_openssl301=${dir_tar}/${name_openssl_301}${common_ext}
file_openssl111m=${dir_tar}/${name_openssl_111m}${common_ext}

minimal_config="no-tests no-dso no-makedepend no-aria no-async no-autoload-config no-blake2 		\
			no-bf no-camellia no-cast no-chacha no-cmac no-cms no-comp no-ct no-des no-dgram no-dh 							\
			no-dsa no-dtls no-ec no-ec2m no-engine no-filenames no-gost no-idea no-mdc2 no-md4 no-multiblock 				\
			no-nextprotoneg no-ocsp no-ocb no-poly1305 no-psk no-rc2 no-rc4 no-rmd160 no-seed no-siphash 					\
			no-sm2 no-sm3 no-sm4 no-srp no-srtp no-ssl3 no-ssl3-method no-ts no-ui-console no-whirlpool 					\
			no-asm -DOPENSSL_SMALL_FOOTPRINT "
_unzip(){
    local file=$1
    local dir_target=$2
    local file_status=${dir_status}/$(basename $file $common_ext).unzip.status
    if [ ! -f $file_status ];then
        echo "[+]unzip $file => $dir_target"
        tar -zxvf $file -C $dir_target
        touch $file_status
    fi
}

_setup_crosstool(){
    export platform=android
    export eabi=aarch64
    export api_level=29
    export HOST_TAG=darwin-x86_64
    export ANDROID_NDK_HOME=/Users/jiuzhu/Documents/android-ndk-r22b
    export SYSROOT=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/$HOST_TAG/sysroot/usr/lib/$eabi-linux-android/$api_level
    export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/$HOST_TAG/bin:$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/$HOST_TAG/bin:$PATH
    export TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/$HOST_TAG
    export AR=$TOOLCHAIN/bin/$eabi-linux-android-ar
    export AS=$TOOLCHAIN/bin/$eabi-linux-android-as
    export CC=$TOOLCHAIN/bin/$eabi-linux-android$api_level-clang
    export CXX=$TOOLCHAIN/bin/$eabi-linux-android$api_level-clang++
    export LD=$TOOLCHAIN/bin/$eabi-linux-android-ld
    export RANLIB=$TOOLCHAIN/bin/$eabi-linux-android-ranlib
    export NM=$TOOLCHAIN/bin/$eabi-linux-android-nm
    export STRIP=$TOOLCHAIN/bin/$eabi-linux-android-strip
    $(CC -v)
}

build_openssl(){
    echo "[+] build openssl ..."
    local file=$1
    local name=$(basename $file $common_ext)
    _unzip $file $dir_build
    cd $dir_build/$name && \
    #  ld: library not found for -lcrt0.o
    local file_status=${dir_status}/$(basename $file $common_ext).build.status
    if [ ! -f $file_status ];then
        ./Configure $minimal_config --prefix=$dir_out/$name || exit
        make -j $(nproc) clean || exit
        make -j $(nproc) || exit
        make -j $(nproc) install || exit
        # rm -rf $dir_out/$name/share || exit
        touch $file_status
    fi
}

build_self(){
    echo "[+] build self ..."
    local openssl_name=$1
    cd $dir_src && \
    
    make clean || exit
    make -j $(nproc)                                    \
        DIR_SROUCE=$dir_src                             \
        DIR_OUT=$dir_out                                \
        MY_CFLAGS="-I $dir_out/$openssl_name/include -I $dir_src/common"   \
	    MY_LDFLAGS="-L $dir_out/$openssl_name/lib" || exit
    make -j $(nproc) execute                            \
        DIR_OUT=$dir_out || exit
}

mkdir -p $dir_build
mkdir -p $dir_status
mkdir -p $dir_out

openssl_selected=$file_openssl301
build_openssl $openssl_selected
openssl_name=$(basename $openssl_selected $common_ext)
build_self $openssl_name
